<?php require_once('../Connections/learnConn.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "guru";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && false) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "../index.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

//tipe=1&nis=111207001&k=8&ks=A&kp=AGM8
if($_POST['tipe']==1){
//echo "test";
	$ul1 = $_POST['ul1'];
	$ul2 = $_POST['ul2'];
	$ul3 = $_POST['ul3'];
	$ul4 = $_POST['ul4'];
	$ul5 = $_POST['ul5'];
	$tgs1 = $_POST['tgs1'];
	$tgs2 = $_POST['tgs2'];
	$tgs3 = $_POST['tgs3'];
	$tgs4 = $_POST['tgs4'];
	$tgs5 = $_POST['tgs5'];
	$uts = $_POST['uts'];
	$uas = $_POST['uas'];
	$totUl = ($ul1 + $ul2 + $ul3 + $ul4 + $ul5) / 5;
	$totUl2 = $totUl * 0.6;
	$totUts = $uts * 0.2;
	$totUas = $uas * 0.2;	 
	$nilai_akhir = $totUl2 + $totUts + $totUas ;	
	$nis = $_POST[nis];
	$kp = $_POST['kd_matpel'];
	$k = $_POST['kd_kelas'];
	$ks = $_POST['kd_kat_kelas'];
	$tahun_ajaran = $_POST['tahun_ajaran'];
	$semester = $_POST['semester'];
		
	// 
	$updateSQL = sprintf("UPDATE nilai SET 
	ul1=$ul1, ul2=$ul2, ul3=$ul3, ul4=$ul4, ul5=$ul5,  
	tgs1=$tgs1, tgs2=$tgs2, tgs3=$tgs3, tgs4=$tgs4, tgs5=$tgs5,
	uts=$uts, uas=$uas, nilai_akhir=$nilai_akhir
	WHERE nis=$nis AND kd_kelas='$k' AND kd_kat_kelas='$ks' AND kd_matpel='$kp' 
	AND tahun_ajaran='$tahun_ajaran' AND semester='$semester'");
						/*                       
                       GetSQLValueString($_POST['uts'], "float"),
                       GetSQLValueString($_POST['uas'], "float"),
                       GetSQLValueString($_POST['nilai_akhir'], "float"),
                       GetSQLValueString($_POST['nis'], "float"),
                       GetSQLValueString($_POST['tahun_ajaran'], "text"),
                       GetSQLValueString($_POST['semester'], "text"));
					   */

  mysql_select_db($database_learnConn, $learnConn);
  $Result1 = mysql_query($updateSQL, $learnConn) or die(mysql_error());

  
  $updateGoTo = "list_siswa.php?kode_matpel=$kp&kode_kelas=$k&kode_sub_kelas=$ks";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
  
}


$colname_listNilai = $_GET['kode_kelas'];
$colname_listNilai2 = $_GET['kode_matpel'];
$colname_listNilai1 = $_GET['kode_sub_kelas'];
//SELECT s.nama, sk.nis, n.* FROM siswa s, nilai n WHERE kd_kelas=%s AND kd_kat_kelas=%s AND kd_matpel=%s
mysql_select_db($database_learnConn, $learnConn);
$query_listNilai = sprintf("SELECT * FROM nilai WHERE kd_kelas=%s AND kd_kat_kelas=%s AND kd_matpel=%s", 
						   GetSQLValueString($colname_listNilai, "int"),
						   GetSQLValueString($colname_listNilai1, "text"),
						   GetSQLValueString($colname_listNilai2, "text"));
$listNilai = mysql_query($query_listNilai, $learnConn) or die(mysql_error());
$row_listNilai = mysql_fetch_assoc($listNilai);
$totalRows_listNilai = mysql_num_rows($listNilai);

$query_listKelas = sprintf("SELECT * FROM kelas WHERE kelas=%s AND sub_kelas=%s", 
						   GetSQLValueString($colname_listNilai, "int"),
						   GetSQLValueString($colname_listNilai1, "text"));

$listKelas = mysql_query($query_listKelas, $learnConn) or die(mysql_error());
$row_listKelas = mysql_fetch_assoc($listKelas);
$totalRows_listKelas = mysql_num_rows($listKelas);

$kd_kls_pn = $row_listKelas['kd_kelas'];
//$query_listSiswa = sprintf("SELECT * FROM siswa_kelas WHERE kd_kelas=%s", 
//						   GetSQLValueString($row_listKelas['kd_kelas'], "text"));
$query_listSiswa = sprintf("SELECT n.*, s.nama FROM nilai n, siswa s WHERE n.nis=s.nis AND kd_kelas=%s AND kd_kat_kelas=%s AND kd_matpel=%s", 						
					   GetSQLValueString($colname_listNilai, "text"),
                       GetSQLValueString($colname_listNilai1, "text"),
                       GetSQLValueString($colname_listNilai2, "text"));
$listSiswa = mysql_query($query_listSiswa, $learnConn) or die(mysql_error());
$row_listSiswa = mysql_fetch_assoc($listSiswa);
$totalRows_listSiswa = mysql_num_rows($listSiswa);

$jk="";

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

	$totUl = ($_POST['ul1'] + $_POST['ul2'] + $_POST['ul3'] + $_POST['ul4'] + $_POST['ul5']) / 5;
	$totUl2 = $totUl * 0.6;
	$totUts = $_POST['uts'] * 0.2;
	$totUas = $_POST['uas'] * 0.2;	 
	$nilai_akhir = $totUl2 + $totUts + $totUas ;
  $insertSQL = sprintf("INSERT INTO nilai (nis, kd_kelas, kd_kat_kelas, kd_matpel, tahun_ajaran, semester, ul1, ul2, ul3, ul4, ul5, tgs1, tgs2, tgs3, tgs4, tgs5, uts, uas, nilai_akhir) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['nis'], "int"),
                       GetSQLValueString($_POST['kd_kelas'], "text"),
                       GetSQLValueString($_POST['kd_kat_kelas'], "text"),
                       GetSQLValueString($_POST['kd_matpel'], "text"),
                       GetSQLValueString($_POST['tahun_ajaran'], "text"),
                       GetSQLValueString($_POST['semester'], "text"),
                       GetSQLValueString($_POST['ul1'], "double"),
                       GetSQLValueString($_POST['ul2'], "double"),
                       GetSQLValueString($_POST['ul3'], "double"),
                       GetSQLValueString($_POST['ul4'], "double"),
                       GetSQLValueString($_POST['ul5'], "double"),
                       GetSQLValueString($_POST['tgs1'], "double"),
                       GetSQLValueString($_POST['tgs2'], "double"),
                       GetSQLValueString($_POST['tgs3'], "double"),
                       GetSQLValueString($_POST['tgs4'], "double"),
                       GetSQLValueString($_POST['tgs5'], "double"),
                       GetSQLValueString($_POST['uts'], "double"),
                       GetSQLValueString($_POST['uas'], "double"),
                       GetSQLValueString($nilai_akhir, "double"));

  mysql_select_db($database_learnConn, $learnConn);
  $Result1 = mysql_query($insertSQL, $learnConn) or die(mysql_error());

  
  
  $kp = $_POST['kd_matpel'];
  $k = $_POST['kd_kelas'];
  $ks = $_POST['kd_kat_kelas'];
  $insertGoTo = "list_siswa.php?kode_matpel=$kp&kode_kelas=$k&kode_sub_kelas=$ks";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}


mysql_select_db($database_learnConn, $learnConn);
$query_thn = "SELECT * FROM tahun_ajaran ORDER BY tahun_ajaran DESC";
$thn = mysql_query($query_thn, $learnConn) or die(mysql_error());
$row_thn = mysql_fetch_assoc($thn);
$totalRows_thn = mysql_num_rows($thn);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Daftar Siswa</title>
<link href="../style/style.css" type="text/css" rel="stylesheet" />
<link href="../style/pro_drop_1.css" type="text/css" rel="stylesheet"/>
<script src="../style/stuHover.js" type="text/javascript"></script>
</head>

<body><center>
<table width="900" bgcolor="#FFFFFF">
	<?php
	include "topmenu_admin.php";
	?>
    <tr>
    	<td id="menu">
        <ul>
        	<li><a href="index.php">Home</a></li>
        	<li><a href="nilai.php">Daftar Kelas</a></li>
   	    <li><a href="jadwal.php">Jadwal</a></li>
        	<li><a href="passE.php">Ubah Password</a></li>
            <li><a href="../logout.php">Logout</a></li>
        </ul>
        </td>
    	<td id="konten"><a href="nilaiA.php?k=<?php echo $_GET['kode_kelas']; ?>&ks=<?php echo $_GET['kode_sub_kelas']; ?>&kp=<?php echo $colname_matpel = $_GET['kode_matpel']; ?>">Tambah Nilai</a><br />
        <table width="100%">
    	  <tr bgcolor="#CCCCCC">
    	    <td>NIS</td>
    	    <td>Nama</td>
    	    <td>UL1</td>
    	    <td>UL2</td>
    	    <td>UL3</td>
    	    <td>UL4</td>
    	    <td>UL5</td>
			<td>TGS1</td>
    	    <td>TGS2</td>
    	    <td>TGS3</td>
    	    <td>TGS4</td>
    	    <td>TGS5</td>
    	    <td>UTS</td>
    	    <td>UAS</td>
    	    <td>Nilai</td>
    	    <td>Aksi</td>
  	    </tr>
			<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form1" id="form1">			  
		Tahun Ajaran
		<select name="tahun_ajaran">
                <?php do { ?>  <option value="<?php echo $row_thn['tahun_ajaran']; ?>"><?php echo $row_thn['tahun_ajaran']; ?></option>
                <?php } while ($row_thn = mysql_fetch_assoc($thn)); ?>
                </select>
			Semester
			<select name="semester">
            	<option value="ganjil">Ganjil</option>
            	<option value="genap">Genap</option>
            </select>
			<tr>
			<tr>
              <td></td>
              <td><?php 			  			    
				mysql_select_db($database_learnConn, $learnConn);
				$query_siswa = sprintf("SELECT sk.nis, s.nama FROM siswa_kelas sk, siswa s WHERE sk.nis=s.nis AND sk.kd_kelas='$kd_kls_pn'");
				$siswa = mysql_query($query_siswa, $learnConn) or die(mysql_error());
				$row_siswa = mysql_fetch_assoc($siswa);
				$totalRows_siswa = mysql_num_rows($siswa);								
				echo "<select name='nis'>
						<option>Pilih</option>";
                 do{
                    //echo "<option value='".$data['idProp']."'>".$data['namaPropinsi']."</option>";
                    echo "<option value='".$row_siswa['nis']."'>".$row_siswa['nama']."</option>";
                 }while ($row_siswa = mysql_fetch_assoc($siswa));
				
				echo "</select>";  
				?></td>
			  <!--
              <td><?php //echo $row_listNilai['kd_kelas']; ?> - <?php //echo $row_listNilai['kd_kat_kelas']; ?></td>
              <td><?php //echo $row_listNilai['kd_matpel']; ?></td> -->
              <td><input type="text" name="ul1" value="<?php echo htmlentities('0', ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
              <td><input type="text" name="ul2" value="<?php echo htmlentities('0', ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
              <td><input type="text" name="ul3" value="<?php echo htmlentities('0', ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
              <td><input type="text" name="ul4" value="<?php echo htmlentities('0', ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
              <td><input type="text" name="ul5" value="<?php echo htmlentities('0', ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
              
			  <td><input type="text" name="tgs1" value="<?php echo htmlentities('0', ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
			  <td><input type="text" name="tgs2" value="<?php echo htmlentities('0', ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
			  <td><input type="text" name="tgs3" value="<?php echo htmlentities('0', ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
			  <td><input type="text" name="tgs4" value="<?php echo htmlentities('0', ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
			  <td><input type="text" name="tgs5" value="<?php echo htmlentities('0', ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
			  
			  <td><input type="text" name="uts" value="<?php echo htmlentities('0', ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
			  <td><input type="text" name="uas" value="<?php echo htmlentities('0', ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
              <td></td>
			  
              <td>
				<input type="hidden" name="kd_kelas" value="<?php echo $_GET['kode_kelas']; ?>" />
				<input type="hidden" name="kd_kat_kelas" value="<?php echo $_GET['kode_sub_kelas']; ?>" />
				<input type="hidden" name="kd_matpel" value="<?php echo $_GET['kode_matpel']; ?>" />
			  <input type="submit" value="Simpan" />
			  <input type="hidden" name="MM_insert" value="form1" />
			  </form>
			</tr>
          <?php do { ?>
			<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" <?php echo "name='form0".$row_listSiswa['nis']."'" ?>>
            <tr>
              <td><?php echo $row_listSiswa['nis']; ?></td>
              <td><?php 			  			    
				echo $row_listSiswa['nama'];
				?></td>
			  <!--
              <td><?php //echo $row_listNilai['kd_kelas']; ?> - <?php //echo $row_listNilai['kd_kat_kelas']; ?></td>
              <td><?php //echo $row_listNilai['kd_matpel']; ?></td> -->
              <td><input type="text" name="ul1" value="<?php echo htmlentities($row_listSiswa['ul1'], ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
              <td><input type="text" name="ul2" value="<?php echo htmlentities($row_listSiswa['ul2'], ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
              <td><input type="text" name="ul3" value="<?php echo htmlentities($row_listSiswa['ul3'], ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
              <td><input type="text" name="ul4" value="<?php echo htmlentities($row_listSiswa['ul4'], ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
              <td><input type="text" name="ul5" value="<?php echo htmlentities($row_listSiswa['ul5'], ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
              
			  <td><input type="text" name="tgs1" value="<?php echo htmlentities($row_listSiswa['tgs1'], ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
			  <td><input type="text" name="tgs2" value="<?php echo htmlentities($row_listSiswa['tgs2'], ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
			  <td><input type="text" name="tgs3" value="<?php echo htmlentities($row_listSiswa['tgs3'], ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
			  <td><input type="text" name="tgs4" value="<?php echo htmlentities($row_listSiswa['tgs4'], ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
			  <td><input type="text" name="tgs5" value="<?php echo htmlentities($row_listSiswa['tgs5'], ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
			  
			  <td><input type="text" name="uts" value="<?php echo htmlentities($row_listSiswa['uts'], ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
			  <td><input type="text" name="uas" value="<?php echo htmlentities($row_listSiswa['uas'], ENT_COMPAT, 'utf-8'); ?>" onchange="hitung()" size="1" /></td>
              <td><?php echo $row_listSiswa['nilai_akhir']; ?></td>
              <td>
              <a href="list_siswa.php?tipe=1&nis=<?php echo $row_listNilai['nis']; ?>&k=<?php echo $row_listNilai['kd_kelas']; ?>&ks=<?php echo $row_listNilai['kd_kat_kelas']; ?>&kp=<?php echo $colname_matpel = $_GET['kode_matpel']; ?>"></a>
			  <input type="submit" value="Update" />
              <a href="nilaiHapus.php?nis=<?php echo $row_listNilai['nis']; ?>&k=<?php echo $row_listNilai['kd_kelas']; ?>&ks=<?php echo $row_listNilai['kd_kat_kelas']; ?>&kp=<?php echo $colname_matpel = $_GET['kode_matpel']; ?>" onClick="return confirm('Apakah Anda benar-benar akan menghapus nilai?')"> &nbsp;&nbsp;&nbsp;<font color='red'>Hapus</font></a>
              </td>
            </tr>
			<input type="hidden" name="tipe" value="1" />
			<input type="hidden" name="nis" value="<?php echo $row_listSiswa['nis']; ?>" />
            <input type="hidden" name="kd_kelas" value="<?php echo htmlentities($row_listSiswa['kd_kelas'], ENT_COMPAT, 'utf-8'); ?>" />
            <input type="hidden" name="kd_kat_kelas" value="<?php echo htmlentities($row_listSiswa['kd_kat_kelas'], ENT_COMPAT, 'utf-8'); ?>" />
            <input type="hidden" name="kd_matpel" value="<?php echo htmlentities($row_listSiswa['kd_matpel'], ENT_COMPAT, 'utf-8'); ?>" />
            <input type="hidden" name="tahun_ajaran" value="<?php echo htmlentities($row_listSiswa['tahun_ajaran'], ENT_COMPAT, 'utf-8'); ?>" />
            <input type="hidden" name="semester" value="<?php echo $row_listSiswa['semester']; ?>" />
            <input type="hidden" name="MM_update" value="form1" />
			</form>
            <?php } while ($row_listSiswa = mysql_fetch_assoc($listSiswa)); ?>
			
              </td>
            </tr>
        </table></td>
    </tr>
    <tr>
    	<td colspan="2" id="footer">Copyright 2012</td>
    </tr>
    
</table></center>
</body>
</html>
<?php
//mysql_free_result($siswa);
//mysql_free_result($listNilai);
?>
